<?php

namespace app\admin\controller;

use think\Controller;
use app\common\model\Singles as M;
use think\Model;

class Singles extends Controller
{

    /**
     * 列表页
     * @return type
     */
    public function index()
    {
        return $this->fetch();
    }

    /*
     * 列表页获取josn数据
     * 
     */

    public function get_josn()
    {
        $m     = new M();
        $rs    = $m->getAll();
        $rs    = $m->getListAll(1);
        $all   = $m->getListAll(2);
        $count = count($all);
        $lists = ['code' => 0, 'msg' => '', 'count' => $count, 'data' => []];
        foreach ($rs as $k => $v)
        {
            $v['status']     = public_status($v['status']);
            $lists['data'][] = $v;
        }
        return $lists;
    }

    /*
     * 列表页获取josn数据
     * 
     */

    public function singles_submit()
    {
        $m = new M();
        if (input('id', 0) == 0)
        {
            $opt = [
                'title'       => input('title'),
                'subtitle'    => input('subtitle'),
                'alias'       => input('alias'),
                'description' => input('description'),
                'keywords'    => input('keywords'),
                'author'      => input('author'),
                'content'     => input('content'),
                'time'        => input('date'),
                'date'        => input('date'),
                'pic'         => input('pic'),
                'status'      => input('status'),
            ];
            $rs  = $m->add($opt);
            return js_return($rs[0], $rs[1]);
        }
        else
        {
            $opt = [
                'title'       => input('title'),
                'subtitle'    => input('subtitle'),
                'description' => input('description'),
                'keywords'    => input('keywords'),
                'author'      => input('author'),
                'content'     => input('content'),
                'time'        => input('date'),
                'date'        => input('date'),
                'pic'         => input('pic'),
                'status'      => input('status'),
            ];
            $rs  = $m->modify($opt, input('id'));
            return js_return($rs[0], $rs[1]);
        }
    }

    /*
     * 添加页面
     * 
     */

    public function add()
    {
        $this->assign('rs');
        return $this->fetch();
    }

    /*
     * 修改页面
     * 
     */

    public function edit()
    {
        $m  = new M();
        $rs = $m->getAll();

        $pid = 0;

        $s = $m->getById(input('id'));

        if (strlen($s->id) != 2)
        {
            $pid = substr($s->id, 0, -2);
        }
        $this->assign('id', input('id'));
        $this->assign('s', $s);
        $this->assign('pid', $pid);
        return $this->fetch();
    }

    /*
     * 删除
     * 
     */

    public function del_submit()
    {
        $m      = new M();
        $id     = input('id');
        $result = $m->destroy($id);
        if (false !== $result)
        {
            return js_return(1, "删除成功！");
        }
        else
        {
            return [0, $this->getError()];
        }
    }

}
